package com.yjz.jvm.java8.time;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Date;


@Slf4j
public class TimeTest {

    @Test
    public void zoneIdTest() {
      System.out.println(LocalDate.now());
      Date startTime = Date.from(LocalDateTime.of(LocalDate.now(), LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant());
      Date endTime = Date.from(LocalDateTime.of(LocalDate.now(), LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
      log.info(ZoneId.getAvailableZoneIds() + "");
      log.info(ZoneId.systemDefault() + "");
      log.info("当前时间戳：{}", Instant.now());
      log.info("当前年月日时间:{}", LocalDateTime.now());
      log.info("当前年月日:{}", LocalDate.now());
      log.info("当前时间:{}", LocalTime.now());
        log.info("日期的加减法***************************");
      log.info("指定日期:{}", LocalDate.of(2016, 5, 18));
        log.info("2个日期之间的天数", Period.between(LocalDate.of(2014, 2, 8), LocalDate.of(2014, 3, 18)).getMonths());
    }

    /**
     * 把当前时间格式为指定格式
     */
    @Test
    public void test5(){
        //获得当前时间
        LocalDateTime localDateTime  = LocalDateTime.now();
        System.out.println(localDateTime);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = localDateTime.format(dtf);
        System.out.println(format);
    }
    /**
     * 把指定字符串格式化为日期
     */
    @Test
    public void test6(){
        String str1="2018-07-05 12:24:12";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(str1, dtf);
        System.out.println(parse);
    }

    // 01. java.util.Date --> java.time.LocalDateTime
    @Test
    public void test7() {
        java.util.Date date = new java.util.Date();
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        System.out.println(localDateTime);
    }

    @Test
    // 04. java.time.LocalDateTime --> java.util.Date
    public void test8() {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        java.util.Date date = Date.from(instant);
        System.out.println(date);
    }

    /**
     * https://www.cnblogs.com/yangzhilong/p/11090146.html
     */

    //2种获取date对象的方法
    @Test
    public void test9(){
        System.out.println(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        System.out.println(new Date());
    }

    //localdatetime的加减法
    @Test
    public void test10(){
        //减去一年
        System.out.println(LocalDate.now().minus(1, ChronoUnit.YEARS));
        //减去一天
        //todo 获取long类型的毫秒数
        System.out.println(LocalDate.now().minus(1,ChronoUnit.DAYS).get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
    }
}
